package com.hospital.controller;

import com.aliyun.oss.ClientException;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.domain.HBloodApplyRecord;
import com.hospital.domain.Hospital;
import com.hospital.service.HospitalService;
import com.hospital.utils.CodeUtil;
import com.hospital.utils.R;
import com.hospital.utils.SmsTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/hospital")
@CrossOrigin(origins = "*",maxAge = 3600)
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;

    String cod = "";

//    @CachePut(value = "hospital",key = "#hospital.hospitalId")
    @PostMapping("/save")
    public R save(@RequestBody Hospital hospital) {
        return R.ok("添加成功").put("list",hospitalService.save(hospital));
    }

//    @Cacheable(value = "hospital",key = "#hospital.hospitalId")
    @PutMapping("/update")
    public R update(@RequestBody Hospital hospital) {
        if(!hospitalService.updateById(hospital))
            return R.error("更新失败");
        else
            return R.ok("更新成功").put("list",hospitalService.updateById(hospital));
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable int id) {
        if(!hospitalService.removeById(id))
            return R.error("删除失败");
        else
            return R.ok("删除成功").put("list",hospitalService.removeById(id));
    }

//    @Cacheable(value = "hospital",key = "#pageNum+'-'+#pageSize")
    @GetMapping("selectPage")
    public R selectPage(@RequestParam(defaultValue = "1") int pageNum,
                        @RequestParam(defaultValue = "3") int pageSize) {
        Page<Hospital> page = new Page<>(pageNum, pageSize);
        Page<Hospital> hospitalPage = hospitalService.page(page);
        List<Hospital> records = hospitalPage.getRecords();
        int total = hospitalService.count();
        return R.ok().put("list", records).put("total",total);
    }

    @PostMapping("/sms")
    public R smsXxs(@RequestBody Hospital hospital, HttpServletRequest request) throws ClientException, com.aliyuncs.exceptions.ClientException {
        Map<String,Object> map = new HashMap<>();
        // 验证码（指定长度的随机数）
        String code = CodeUtil.generateVerifyCode(6);
        cod=code;
        String TemplateParam = "{\"code\":\""+code+"\"}";
        // 短信模板id
        String TemplateCode = "SMS_471370438";
        SendSmsResponse response = SmsTool.sendSms(hospital.getPhone(),TemplateParam,TemplateCode);
        map.put("verifyCode",code);
        map.put("phone",hospital.getPhone());
        request.getSession().setAttribute("CodePhone",map);
        return R.ok("验证码发送成功").put("code", code);
    }

    @PostMapping("/login")
    public R login(@RequestBody Hospital hospital) {
        if(hospital.getVerifyCode().equals(cod))
            return R.ok("登录成功").put("success",1);
        else
            return R.error("验证码错误").put("success",0);
    }

}
